// pages/myReservations/myReservations.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    reserveTeam: []
  },
  getReservations: function () {
    wx.request({
      url: 'http://' + app.globalData.ip + '/reservation/getUserReservationsTeamShow',
      data: { userID: app.globalData.userInfo.openId },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: (res) => {
        console.log(res)
        var teams = res.data.reservations
        for (var i = 0; i < teams.length; ++i) {
          var d = new Date(teams[i]['createTime'])
          teams[i]['createTime'] = d.getFullYear() + '.' + d.getMonth() + '.' + d.getDate()
        }
        this.setData({
          reserveTeam: teams
        })
      },
      fail: () => {
        wx.showModal({
          content: '服务端错误',
        })
      }
    })
  },

  lookDetail: function (e) {
    var index = e.currentTarget.dataset.teamindex
    app.globalData.team = this.data.reserveTeam[index]
    wx.navigateTo({
      url: '../teamDetail/teamDetail',
    })
  },

  /**点击收藏功能 */
  like: function (e) {

    if (app.globalData.hasUserInfo) {
      //用户授权过了
      var index = e.currentTarget.dataset.idx
      var teamID = e.currentTarget.dataset.teamid
      var that = this
      var mylike = "teams[" + index + "].like"
      if (this.data.teams[index].like == true) {
        //原先为true表示收藏，则点击时表示取消收藏     
        /**向后端发送请求 取消当前的收藏 */
        wx.request({
          url: 'http://' + app.globalData.ip + '/like/cancleLike',
          data: { userid: app.globalData.userInfo.openId, teamid: teamID },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: function (res) {
            if (res.data.msg == 1) {
              wx.showToast({
                title: '取消收藏',
              })
              that.setData({
                [mylike]: false
              })
            } else {
              wx.showModal({
                content: '服务端取消收藏失败',
              })
            }
          },
          fail: function () {
            wx.showModal({
              content: '取消收藏失败',
            })
          }
        })
      } else {
        /**向后端发送请求将当前的收藏 */
        wx.request({
          url: 'http://' + app.globalData.ip + '/like/addLike',
          data: { userid: app.globalData.userInfo.openId, teamid: teamID },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: function (res) {
            if (res.data.msg == 1) {
              wx.showToast({
                title: '收藏成功',
              })
              that.setData({
                [mylike]: true
              })
            } else {
              wx.showModal({
                content: '服务端收藏失败'
              })
            }
          },
          fail: function () {
            wx.showModal({
              content: '收藏失败'
            })
          }
        })
      }

    } 
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getReservations()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})